﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum ComboType {
	NORMAL, //Single line with 3 block
	LINE_4, //Single line with 4 block
	LINE_L, //Duel line with L shape blocks
	LINE_T, //Duel line with T shape blocks
	LINE_P, //Duel line with + shape blocks
	LINE_S //Single line with 6 block
}

public class Core : Singleton<Core> {

	//Block prefab
	public GameObject blockPrefab;

	//Level data
	private int gridSize = 8;
	private Block[,] blocksGrid;

	void Start () {
		Init();
	}

	public void Init () {

		//Init pool
		Pool.Instance.Init(blockPrefab, gridSize*gridSize);

		//Init grid
		blocksGrid = new Block[gridSize,gridSize];
		for(int col=0; col<gridSize; col++) {
			for(int row=0; row<gridSize; row++) {
				blocksGrid[row,col] = Pool.Instance.Pick();
			}
		}
	}

	public MatchedList Match3 (Block target) {

		bool hMatched = false; //Matched in horizontal line
		bool vMatched = false; //Matched in vertical line

		//Check by row
		if(target.Row-1 > -1 && target.Row+1 < gridSize) {
			if(target == blocksGrid[target.Row-1, target.Column] && target == blocksGrid[target.Row+1, target.Column]) {
				hMatched = true;
			}
		}
		
		if(target.Row+1 < gridSize && target.Row+2 < gridSize) {
			if(target == blocksGrid[target.Row+1, target.Column] && target == blocksGrid[target.Row+2, target.Column]) {
				hMatched = true;
			}
		}
		
		if(target.Row-1 > -1 && target.Row-2 > -1) {
			if(target == blocksGrid[target.Row-1, target.Column] && target == blocksGrid[target.Row-2, target.Column]) {
				hMatched = true;
			}
		}
		
		//Check by column
		if(target.Column-1 > -1 && target.Column+1 < gridSize) {
			if(target == blocksGrid[target.Row, target.Column-1] && target == blocksGrid[target.Row, target.Column+1]) {
				vMatched = true;
			}
		}
		
		if(target.Column+1 < gridSize && target.Column+2 < gridSize) {
			if(target == blocksGrid[target.Row, target.Column+1] && target == blocksGrid[target.Row, target.Column+2]) {
				vMatched = true;
			}
		}
		
		if(target.Column-1 > -1 && target.Column-2 > -1) {
			if(target == blocksGrid[target.Row, target.Column-1] && target == blocksGrid[target.Row, target.Column-2]) {
				vMatched = true;
			}
		}

		//Get list that matched
		List<Block> matchedV = new List<Block>();
		List<Block> matchedH = new List<Block>();

		if(vMatched) {
			matchedV = GetAllMatchedBlock(target, false);
		}

		if(hMatched) {
			matchedH = GetAllMatchedBlock(target, true);
		}

		MatchedList matchedList = new MatchedList(matchedH, matchedV, ComboType.NORMAL);
		return matchedList;
	}

	//Destroy all macthed slot by row and column from target slot
	private List<Block> GetAllMatchedBlock (Block target, bool hMatched) {

		List<Block> matched = new List<Block>();

		if(hMatched) { //Get all matched in rows
			
			for(int i=target.Row+1; i<gridSize; i++) {
				if(blocksGrid[i,target.Column] != target) {
					break;
				}
				
				matched.Add(blocksGrid[i,target.Column]);
			}
			
			for(int i=target.Row-1; i>=0; i--) {
				if(blocksGrid[i,target.Column] != target) {
					break;
				}
				
				matched.Add(blocksGrid[i, target.Column]);
			}
			
		}
		else { //Get all matched by columns
			for(int i=target.Column+1; i<gridSize; i++) {
				
				if(blocksGrid[target.Row,i] != target) {
					break;
				}
				
				matched.Add(blocksGrid[target.Row, i]);
			}
			
			for(int i=target.Column-1; i>=0; i--) {
				
				if(blocksGrid[target.Row,i] != target) {
					break;
				}
				
				matched.Add(blocksGrid[target.Row, i]);
			}
		}
		
		matched.Add(target);

		return matched;
	}
}